package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorRegister;
import org.jeecg.common.modules.doctor.entity.DoctorRegisterAccount;
import org.jeecg.common.modules.doctor.entity.DoctorRegisterRecord;
import org.jeecg.common.modules.doctor.mapper.DoctorRegisterRecordMapper;
import org.jeecg.common.modules.doctor.service.IDoctorRegisterRecordService;
import org.jeecg.common.modules.doctor.service.IDoctorRegisterService;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterOrderVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterRecordListVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterRecordSysListVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterRecordSysSettleVo;
import org.jeecg.common.modules.register.entity.RegisterPatient;
import org.jeecg.common.modules.register.service.IRegisterPatientService;
import org.jeecg.common.util.enums.RegisterEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import javax.print.Doc;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: doctor_register_record
 * @Author: jeecg-boot
 * @Date:   2020-12-23
 * @Version: V1.0
 */
@Slf4j
@Service
public class DoctorRegisterRecordServiceImpl extends ServiceImpl<DoctorRegisterRecordMapper, DoctorRegisterRecord> implements IDoctorRegisterRecordService {
    @Resource
    private IDoctorRegisterService doctorRegisterService;
    @Resource
    private IDoctorRegisterRecordService doctorRegisterRecordService;
    @Resource
    private IDoctorService doctorService;
    @Resource
    private DoctorRegisterRecordMapper doctorRegisterRecordMapper;
    @Resource
    private IRegisterPatientService registerPatientService;

    @Override
    public int getRecordCount(String doctorRegisterId){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,doctorRegisterId);
        queryWrapper.isNotNull(DoctorRegisterRecord::getPatientId);
        queryWrapper.isNotNull(DoctorRegisterRecord::getOpenid);
        queryWrapper.in(DoctorRegisterRecord::getStatus,1,2,3);  //挂号中：1；已支付：2；挂号成功：3；
        return count(queryWrapper);
    }

    @Override
    public RegisterPatient registerPatientCheck(String openId){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getOpenid,openId);
        queryWrapper.eq(DoctorRegisterRecord::getStatus, RegisterEnum.SUCCESS.getCode());
        queryWrapper.orderByDesc(DoctorRegisterRecord::getRegisterTime);
        List<DoctorRegisterRecord> doctorRegisterRecord = list(queryWrapper);
        if(doctorRegisterRecord.size() > 0){
            // 获取该挂号单对应的患者档案信息（过滤删除的档案）
            return registerPatientService.getPatientOne(doctorRegisterRecord.get(0).getPatientId(),0);
        }
        return null;
    }

    @Override
    public DoctorRegisterRecord getSureRecord(String doctorRegisterId){
//        int alreadyCount = getRecordCount(doctorRegisterId);
//
//        //获取开放的挂号数量
//        DoctorRegister doctorRegister = doctorRegisterService.getById(doctorRegisterId);
//        int count = doctorRegister.getCount();
//
//        if(alreadyCount >= count){
//            return 0;
//        }
//
//        return alreadyCount + 1;
        log.info("doctorRegisterId：{}",doctorRegisterId);

        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,doctorRegisterId);
        queryWrapper.eq(DoctorRegisterRecord::getStatus,0);
        queryWrapper.orderByAsc(DoctorRegisterRecord::getVisitNum);

        //获取第一个序号
        List<DoctorRegisterRecord> doctorRegisterRecords = list(queryWrapper);
        log.info("doctorRegisterRecords：{}",doctorRegisterRecords);
        return doctorRegisterRecords.get(0);
    }

    @Override
    public boolean checkRegisterSame(String doctorRegisterId,String openId){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,doctorRegisterId);
        queryWrapper.eq(DoctorRegisterRecord::getOpenid,openId);
        queryWrapper.in(DoctorRegisterRecord::getStatus, 1,2); // 1：待处理（已支付）；2 挂号通过
        if(count(queryWrapper) == 0){
            return false;
        }else{
            return true;
        }
    }
    @Override
    public boolean checkRegisterSame(String doctorRegisterId, String openId, String parentId){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,doctorRegisterId);
        queryWrapper.eq(DoctorRegisterRecord::getOpenid,openId);
        queryWrapper.eq(DoctorRegisterRecord::getPatientId,parentId);
        queryWrapper.in(DoctorRegisterRecord::getStatus, 1,2); // 1：待处理（已支付）；2 挂号通过
        if(count(queryWrapper) == 0){
            return false;
        }else{
            return true;
        }
    }
    @Override
    public boolean checkRegister(String doctorRegisterId,Integer visit_num){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,doctorRegisterId);
        queryWrapper.eq(DoctorRegisterRecord::getVisitNum,visit_num);
        queryWrapper.in(DoctorRegisterRecord::getStatus, 1,2,3);
        if(count(queryWrapper) == 0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public List<DoctorRegisterRecordListVo> getRecordByOpenid(String openid,String nowDate,String beforeThirtyDate, String status){
        return doctorRegisterRecordMapper.getRegisterRecord(openid,nowDate,beforeThirtyDate,status);
    }

    @Override
    public DoctorRegisterRecordListVo getRecordById(String recordId){
        return doctorRegisterRecordMapper.getRegisterRecordOne(recordId);
    }

    @Override
    public DoctorRegister getRegisterByRecordId(String registerId){
//        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(DoctorRegisterRecord::getId,registerId);
        DoctorRegisterRecord registerRecord = getById(registerId);

        return doctorRegisterService.getById(registerRecord.getDoctorRegisterId());
    }

    @Override
    public List<DoctorRegisterRecordSysListVo> getRegisterRecordSysList(String doctorName, String patientName, String visitTime, Integer status, Integer dataStart, Integer pageSize){
        List<DoctorRegisterRecordSysListVo> doctorRegisterRecordSysListVos = doctorRegisterRecordMapper.getRegisterRecordSysList(doctorName,patientName,visitTime,status,dataStart,pageSize);
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        String createTime = "";
        //复制对象且转换日期到字符串
        for(DoctorRegisterRecordSysListVo doctorRegisterRecordSysListVo:doctorRegisterRecordSysListVos){
            createTime = sdf.format(doctorRegisterRecordSysListVo.getRegisterTime());
            log.info("转化的时间：{}",createTime);
            doctorRegisterRecordSysListVo.setRegisterTimeStr(createTime);
        }
        return doctorRegisterRecordSysListVos;
    }

    @Override
    public List<DoctorRegisterRecord> getRegisterRecordList(String doctorRegisterId){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,doctorRegisterId);
        queryWrapper.ne(DoctorRegisterRecord::getStatus,0);  // 不是0：意味着已经有人进行了挂号操作
        return list(queryWrapper);
    }

    @Override
    public List<DoctorRegisterRecord> getRegisterRecordListAll(String doctorRegisterId){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,doctorRegisterId);
        return list(queryWrapper);
    }

    @Override
    public int getRegisterRecordSysCount(String doctorName, String patientName, String visitTime, Integer status){
        return doctorRegisterRecordMapper.getRegisterRecordSysCount(doctorName,patientName,visitTime,status);
    }

    @Override
    public DoctorRegisterRecordSysListVo getRegisterRecordSysOne(String recordId){
        return doctorRegisterRecordMapper.getRegisterRecordSysOne(recordId);
    }

    @Override
    public DoctorRegisterRecordSysSettleVo getRecordSettle(String recordId){
        log.info("挂号单id：{}",recordId);
        DoctorRegisterRecordSysSettleVo settleVo = doctorRegisterRecordMapper.getRecordSettleInfo(recordId);

        log.info("结算数据表数据：{}",settleVo);
        BigDecimal hundred = new BigDecimal(100);
        //渠道收益 = 渠道分成比例 * 挂号费用
        BigDecimal hospitalShare = BigDecimal.valueOf(settleVo.getHospitalShare());
        BigDecimal hospitalProfit = (settleVo.getRegisterPrice().multiply(hospitalShare)).divide(hundred);
        hospitalProfit = hospitalProfit.setScale(2, RoundingMode.HALF_UP);
        settleVo.setHospitalProfit(hospitalProfit);
        log.info("渠道收益：{}",hospitalProfit);

        //医生收益 = 医生挂号分成比例 * 挂号费用
        BigDecimal registerRatio = BigDecimal.valueOf(settleVo.getRegisterRatio());
        BigDecimal doctorProfit = (settleVo.getRegisterPrice().multiply(registerRatio)).divide(hundred);
        doctorProfit = doctorProfit.setScale(2, RoundingMode.HALF_UP);
        settleVo.setDoctorProfit(doctorProfit);
        log.info("医生收益：{}",doctorProfit);
        return settleVo;
    }

    @Override
    public DoctorRegisterRecord checkSure(String openId,String doctorRegisterId){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getOpenid,openId);
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,doctorRegisterId);
        queryWrapper.in(DoctorRegisterRecord::getStatus,1,2);  // 1：待处理（已支付）；2：挂号成功
        return getOne(queryWrapper);
    }
    @Override
    public List<DoctorRegisterRecordListVo>  getRegisterRecordTime(String doctorId, String visitTim, String registerHospitalId){
        return doctorRegisterRecordMapper.getRegisterRecordTime(doctorId, visitTim,registerHospitalId);
    }
    @Override
    public List<DoctorRegisterRecord> getDoctorRegisterRecord(String id){
        LambdaQueryWrapper<DoctorRegisterRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegisterRecord::getDoctorRegisterId,id);
        queryWrapper.in(DoctorRegisterRecord::getStatus,1,2);  // 1：待处理（已支付）；2：挂号成功
        return list(queryWrapper);
    }
}
